import json
import random
import os
from abc import abstractmethod, ABC

from services.PageInfo import *
import utils.mylog as logging



class InputPolicy(ABC):
    """
    输入事件获取策略
    """
    def __init__(self, input_manager):
        self.input_manager = input_manager
        self.device = input_manager.device
        self.driver = self.device.driver
        self.logger = logging.getLogger(self.__class__.__name__)
        self.logger.info("finished init")

    @abstractmethod
    def get_next_option(self):
        # TODO:不超出应用范围判断
        pass

class RandomInputPolicy(InputPolicy):
    """
    随机策略
    """
    def __init__(self, input_manager):
        super().__init__(input_manager)

    def get_next_option(self):
        widgets = self.input_manager.current_page_status.op_widgets
        if widgets == []:
            self.logger.warning("无可操作控件,返回上一个页面")
            return Widget("back")
        random_number = random.randint(0, len(widgets) - 1)
        return widgets[random_number]